﻿using System;
using System.Web.UI.WebControls;
using System.Web;

namespace Dooshion.Utility
{
    /// <summary>
    /// 数据绑定帮助类
    /// </summary>
    public class DataBindHelper
    {
        /// <summary>
        /// Repeater的简单绑定
        /// </summary>
        /// <param name="obj">数据源</param>
        /// <param name="myrepeater">绑定的控件名</param>
        public static void BindRepeater(Object obj, Repeater rpt)
        {
            rpt.DataSource = obj;
            rpt.DataBind();
        }

        /// <summary>
        /// 绑定DropDownList控件
        /// </summary>
        /// <param name="dataTextField">text值</param>
        /// <param name="dataValueField">value值</param>
        /// <param name="obj">数据对象</param>
        /// <param name="myDropDownList">控件名</param>
        /// <param name="defaultText">默认的选择项文本</param>
        /// <param name="defaultValue">默认的选择项值</param>
        public static void BindDropDownList(string dataTextField, string dataValueField, Object obj, DropDownList myDropDownList, string defaultText, string defaultValue)
        {
            myDropDownList.DataSource = obj;
            myDropDownList.DataValueField = dataValueField;
            myDropDownList.DataTextField = dataTextField;
            myDropDownList.DataBind();
            myDropDownList.Items.Insert(0, new ListItem(defaultText, defaultValue));
        }

        /// <summary>
        /// 绑定DropDownList控件
        /// </summary>
        /// <param name="dataTextField">text值</param>
        /// <param name="dataValueField">value值</param>
        /// <param name="obj">数据对象</param>
        /// <param name="myDropDownList">控件名</param>
        public static void BindDropDownList(string dataTextField, string dataValueField, Object obj, DropDownList myDropDownList)
        {
            myDropDownList.DataSource = obj;
            myDropDownList.DataValueField = dataValueField;
            myDropDownList.DataTextField = dataTextField;
            myDropDownList.DataBind();
        }

        /// <summary>
        /// DropDownList选中指定的值
        /// </summary>
        /// <param name="selectValue">要选中项目的值</param>
        /// <param name="myDropDownList">下拉列表框的名称</param>
        public static void SelectedDropDownListValue(DropDownList myDropDownList, string selectValue)
        {
            myDropDownList.SelectedIndex = myDropDownList.Items.IndexOf(myDropDownList.Items.FindByValue(selectValue));
        }


        /// <summary>
        /// DropDownList选中指定的文本
        /// </summary>
        /// <param name="selectText">要选中项目的文本</param>
        /// <param name="myDropDownList">下拉列表框的名称</param>
        public static void SelectedDropDownListText(DropDownList myDropDownList, string selectText)
        {
            myDropDownList.SelectedIndex = myDropDownList.Items.IndexOf(myDropDownList.Items.FindByText(selectText));
        }

        /// <summary>
        /// 绑定ListBox控件
        /// </summary>
        /// <param name="dataTextField">text值</param>
        /// <param name="dataValueField">value值</param>
        /// <param name="obj">数据对象</param>
        /// <param name="myListBox">控件名</param>
        public static void BindListBox(string dataTextField, string dataValueField, Object obj, ListBox myListBox)
        {
            myListBox.DataSource = obj;
            myListBox.DataValueField = dataValueField;
            myListBox.DataTextField = dataTextField;
            myListBox.DataBind();
        }
        /// <summary>
        /// 绑定CheckBoxList控件
        /// </summary>
        /// <param name="dataTextField">text值</param>
        /// <param name="dataValueField">value值</param>
        /// <param name="obj">数据对象</param>
        /// <param name="myCheckBoxList">控件名</param>
        public static void BindCheckBoxList(string dataTextField, string dataValueField, Object obj, CheckBoxList myCheckBoxList)
        {
            myCheckBoxList.DataSource = obj;
            myCheckBoxList.DataValueField = dataValueField;
            myCheckBoxList.DataTextField = dataTextField;
            myCheckBoxList.DataBind();
        }
        /// <summary>
        /// 绑定RadioButtonList控件
        /// </summary>
        /// <param name="dataTextField">text值</param>
        /// <param name="dataValueField">value值</param>
        /// <param name="obj">数据对象</param>
        /// <param name="myDropDownList">控件名</param>
        public static void BindRadioButtonList(string dataTextField, string dataValueField, Object obj, RadioButtonList myDropDownList)
        {
            myDropDownList.DataSource = obj;
            myDropDownList.DataValueField = dataValueField;
            myDropDownList.DataTextField = dataTextField;
            myDropDownList.DataBind();
        }


    }
}
